<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2019/3/17
 * Time: 下午2:00
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Template extends Model
{
    protected $primaryKey = 'template_id';
    protected $table = 'template';
    protected $fillable = ['grid1','grid2','grid3','grid4','grid5','grid6','template_id','updated_at','created_at','name','template_name'];

    /**添加或编辑模板
     * @param $postdata
     */
    public static function save_template($postdata)
    {
        if(empty($postdata['template_id'])){
            $res = self::query()->create($postdata);
        }else{
            $templte_model = self::query()->find($postdata['template_id']);
            unset($postdata['_token']);
            $res = $templte_model -> update($postdata);
        }
        return $res;
    }

    /**删除模板
     * @param $template_id  模板id
     */
    public static function del_template($template_id)
    {
        return self::query()->find($template_id)->delete();
    }

    /**
     * 后台模板列表
     */
    public static function list()
    {

        $query = self::query()
            ->select(
                'template_id',
                't1.goods_name AS grid1',
                't2.goods_name AS grid2',
                't3.goods_name AS grid3',
                't4.goods_name AS grid4',
                't5.goods_name AS grid5',
                't6.goods_name AS grid6',
                'template.grid1 AS box1',
                'template.grid2 AS box2',
                'template.grid3 AS box3',
                'template.grid4 AS box4',
                'template.grid5 AS box5',
                'template.grid6 AS box6'
            );
       return (new static()) -> public_join($query) -> get();
    }

    /*
     * 前台商品列表
     */
    public static function api_front_list()
    {
        $goods_list = [];
        $template_list = self::query()
            -> select('grid1','grid2','grid3','grid4','grid5','grid6')
            -> first()->toArray();

        foreach($template_list as $grid => $goods_id){
            $goods_info =  self::query()
                ->from('goods')
                ->select('*','goods_name AS name')
                ->where('marketable','=',1)
                ->selectRaw(DB::raw('CONCAT("'.''.'",image) AS image'))
                ->where('disabled','=',0)
                ->where('marketable','=',1)
                ->where('goods_id','=',$goods_id)
                ->orderBy('sort','desc')
                ->first();
            if(empty($goods_info)){
               continue;
            }
            $goods_info = $goods_info ->toArray();
            $goods_info['grid'] = $grid;
            $goods_list[] = $goods_info;
        }
        return $goods_list;
    }

    /**
     * @param $query  \Illuminate\Database\Eloquent\Builder
     * @return mixed \Illuminate\Database\Query\Builder|static
     */
    public function public_join($query)
    {
        return $query
            ->leftJoin('goods AS t1','grid1','=','t1.goods_id')
            ->leftJoin('goods AS t2','grid2','=','t2.goods_id')
            ->leftJoin('goods AS t3','grid3','=','t3.goods_id')
            ->leftJoin('goods As t4','grid4','=','t4.goods_id')
            ->leftJoin('goods As t5','grid5','=','t5.goods_id')
            ->leftJoin('goods As t6','grid6','=','t6.goods_id');
    }


}